package com.csi.sai.bl.dataview;

import java.util.ArrayList;

import javax.sql.DataSource;

import com.csi.sai.data.entities.Cliente;
import com.csi.sai.utilities.Utilities;
import com.csi.sai.utilities.templates.reports.ClientTemplate;

public class GetDataViewClients extends GetDataView {

	private String headerHTML;
	private String bodyHTML;
	
	public GetDataViewClients(DataSource ds) {
		super(ds);
	}

	@Override
	protected void writeHeaders() {
		headerHTML = ClientTemplate.headers;

	}

	@Override
	protected <T> void writeBody(Generics<T> object) {
		ArrayList<Cliente> listaClientes = (ArrayList<Cliente>)object.getObject();
		StringBuffer sb = new StringBuffer();
		for(Cliente cliente : listaClientes) {
			String row = ClientTemplate.row;
			row = row.replace("{idClient}", ""+cliente.getIdCliente()).
				replace("{client}", cliente.getNombre()).
				replace("{address}", cliente.getDireccion()).
				replace("{web}", cliente.getPaginaWeb()).
				replace("{creation}", Utilities.getDateByFormat(cliente.getFechaAlta(), Utilities.formatView)).
				replace("{status}", cliente.getEstatus())
				.replace("{title}", cliente.getNombre());
			sb.append(row);
		}
		bodyHTML = ClientTemplate.body.replace("{body}", sb.toString());
	}

	@Override
	public String getHTMLCode() {
		return headerHTML.replace("{body}", bodyHTML);
	}

}
